import math
import random

import const


class Ball:
    def __init__(self, x, y, radis=10, direction=0.0, speed=5.0):
        self.x = x
        self.y = y
        self.radis = radis
        self.direction = direction
        self.speed = speed

    def move(self):
        self.x += math.cos(self.direction) * self.speed
        self.y += math.sin(self.direction) * self.speed
        if self.x < self.radis:
            self.x = self.radis
            if self.direction <= math.pi:
                self.direction = math.pi - self.direction
            else:
                self.direction = math.pi * 3 - self.direction
        if self.y < self.radis:
            self.y = self.radis
            self.direction = math.pi * 2 - self.direction

        if self.x > const.width - self.radis:
            self.x = const.width - self.radis
            if self.direction <= math.pi / 2:
                self.direction = math.pi - self.direction
            else:
                self.direction = math.pi * 3 - self.direction
        if self.y > const.height - self.radis:
            self.y = const.height - self.radis
            self.direction = math.pi * 2 - self.direction

    def __str__(self):
        return 'direction:%s, speed:%s, x:%s, y:%s, radis:%s' % (self.direction, self.speed, self.x, self.y, self.radis)


def get_random_direction():
    num = random.randrange(1, 4)
    return random.Random().random() * math.pi / 2 * num


if __name__ == '__main__':
    # for i in range(100):
    #     dir_ = get_random_direction()
    #     print(dir_)
    # exit(0)
    for i in range(1, 5):
        v = Ball(direction=math.pi / 4 * i, x=const.width/2, y=const.height/2)

        for j in range(10):
            v.move()
            print(v)
        print('==========')
